package InterFaceContact;

import interfaceGrafica.Acoes;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class ReadData {
	
	static String path;
	
	public static void readFromFile (String p) throws IOException{
		path = p;
		ReadData.readPositions();
		ReadData.readSimulation();
		ReadData.readNetwork();
		ReadData.readTrafic();
	}
	
	public static void readPositions() throws IOException{
		FileReader file = new FileReader(ReadData.path+"/position");
	    BufferedReader in = new BufferedReader(file);
	    
	    String aux = in.readLine();
	    ArquivosConfiguracao.nameSimulation = aux;
	    aux = in.readLine();
	    
	    if(aux.equals("Positions:")){
	    	
	    	aux = in.readLine();
	    	while(!aux.equals("Lig:")){
	    		String aux2[] = aux.split(" ");
	    		Position.getPos().put(aux2[0], new Double[2]);
	    		Position.getPos().get(aux2[0])[0] = Double.parseDouble(aux2[1]);
	    		Position.getPos().get(aux2[0])[1] = Double.parseDouble(aux2[2]);
	    		aux = in.readLine();
	    	}
	    	
	    	aux = in.readLine();
	    	while(aux != null){
	    		String aux2[] = aux.split(" ");
	    		
	    		if(Position.getLig().containsKey(aux2[0])){
	    			Position.getLig().get(aux2[0]).add(aux2[1]);
	    		}else{
	    			Position.getLig().put(aux2[0], new ArrayList<String>());
	    			Position.getLig().get(aux2[0]).add(aux2[1]);
	    		}
	    		
	    		aux = in.readLine();
	    	}
	    }else{
	    	System.out.println("Erro na leitura das posicoes !!");
	    }
	    
	    in.close();
	    file.close();
	}
	
	public static void readSimulation() throws IOException{
		FileReader file = new FileReader(ReadData.path+"/simulation");
	    BufferedReader in = new BufferedReader(file);
	    
	    in.readLine();
	    String aux[] = in.readLine().split(":");
	    Simulation.setNumRequest(aux[1]);
	    aux = in.readLine().split(":");
	    aux = aux[1].split(";");
	    Simulation.setRsaType(aux[0]);
	    Simulation.setRsaRouting(aux[1]);
	    Simulation.setRsaPolicy(aux[2]);
	    aux = in.readLine().split(":");
	    Simulation.setLoadPoints(aux[1]);
	    aux = in.readLine().split(":");
	    Simulation.setReplic(aux[1]);
	    aux = in.readLine().split(":");
	    Simulation.setSig(aux[1]);
	    
	    
	    
	    in.close();
	    file.close();
		
	}
	
	public static void readNetwork() throws IOException{
		FileReader file = new FileReader(ReadData.path+"/network");
	    BufferedReader in = new BufferedReader(file);
	    
	    String aux = in.readLine();
	    if(aux.equals("node:")){
	    	aux = in.readLine();
	    	while(!aux.equals("links:")){
	    		Acoes.topologia.put(aux, new HashMap<String, Link>());
	    		aux = in.readLine();
	    	}
	    	
	    	aux = in.readLine();
	    	while(aux!=null){
		    	String aux2[] = aux.split(";");
		    	Link link = new Link(aux2[5], aux2[2], aux2[3], aux2[4]);
		    	
		    	Acoes.topologia.get(aux2[0]).put(aux2[1], link);
		    	aux = in.readLine();
	    	}

	    }else{
	    	System.out.println("Erro na leitura das posicoes!");
	    }
	    in.close();
	    file.close();
	    
	    Network.nos = new HashMap<String, HashMap<String, Link> > (); 
	    Network.setNos(Acoes.topologia);
	    
	}
	
	public static void readTrafic() throws IOException{
		FileReader file = new FileReader(ReadData.path+"/trafic");
	    BufferedReader in = new BufferedReader(file);
	    Request re;
	    
	    String temp = in.readLine();
	    String temp1 = new String();
	    String temp2 = new String();
	  
	    while(temp != null){
	    	if(temp.split("->").length == 2){
	    		String temp3[] = temp.split("->");
	    		temp1 = temp3[0];temp2 = temp3[1];
//	    		System.out.println(temp1+" -> " +temp2);
	    		if(!Trafic.config.containsKey(temp1))Trafic.config.put(temp1, new HashMap< String, ArrayList<Request> > ());
	    	}else{
	    		temp = temp.trim();
	    		String temp4[] = temp.split(";");
	    		re = new Request();
	    		re.setSize(temp4[0]);
	    		re.setArrive(temp4[1]);
	    		re.setAtend(temp4[2]);
	    		re.setIncr(temp4[3]);
	    		if(!Trafic.config.get(temp1).containsKey(temp2)){
	    			Trafic.config.get(temp1).put(temp2, new ArrayList<Request>());
	    			Trafic.config.get(temp1).get(temp2).add(re);
	    		}else{
	    			Trafic.config.get(temp1).get(temp2).add(re);
	    		}
	    		
	    	}
	    	temp = in.readLine();
	    }
	    in.close();
	    file.close();
	    
	}
}
